/*
* acooly.cn Inc.
* Copyright (c) 2017 All Rights Reserved.
* create by ouwen
* date:2017-09-26
*/
package com.yiji.fintech.musfinance.web;

import com.acooly.core.common.web.AbstractJQueryEntityController;
import com.acooly.core.utils.enums.ResultStatus;
import com.acooly.core.utils.security.JWTUtils;
import com.acooly.module.security.domain.User;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.yiji.fintech.musfinance.base.enums.*;
import com.yiji.fintech.musfinance.common.enums.ReviewStatusEnum;
import com.yiji.fintech.musfinance.entity.TransactionWithdraw;
import com.yiji.fintech.musfinance.service.TransactionWithdrawService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * center_transaction_withdraw 管理控制器
 * 
 * @author ouwen
 * Date: 2017-09-26 14:23:24
 */
@Controller
@RequestMapping(value = "/manage/withdraw/transactionWithdraw")
public class TransactionWithdrawManagerController extends AbstractJQueryEntityController<TransactionWithdraw, TransactionWithdrawService> {
	

	{
		allowMapping = "*";
	}
	@Autowired
	private TransactionWithdrawService transactionWithdrawService;

	@Override
	protected TransactionWithdraw doSave(HttpServletRequest request, HttpServletResponse response, Model model, boolean isCreate) throws Exception {
		User user = (User) request.getAttribute(JWTUtils.CLAIMS_KEY_SUB);
		TransactionWithdraw withdrawDoRequest = new TransactionWithdraw();
		this.doDataBinding(request, withdrawDoRequest);
		TransactionWithdraw withdrawDoDb = loadEntity(request);
		ReviewStatusEnum reviewStatus = withdrawDoRequest.getReviewStatus();
		if( user == null){
			throw new IllegalArgumentException("操作者信息获取失败");
		}
		if(withdrawDoDb!=null){
			withdrawDoDb.setOperaterId(String.valueOf(user.getId()));
			withdrawDoDb.setOperaterName(user.getRealName());
			withdrawDoDb.setOperaterTime(new Date());
			withdrawDoDb.setRemark(withdrawDoRequest.getRemark());
			//不能重复审核
			if(Lists.newArrayList(ReviewStatusEnum.REVIEW_FAIL,ReviewStatusEnum.REVIEW_SUCCESS).contains(withdrawDoDb.getReviewStatus())){
				throw new IllegalArgumentException("不能重复审核");
			}
		}
		withdrawDoDb.setReviewStatus(reviewStatus);
		transactionWithdrawService.update(withdrawDoDb);
		if(ReviewStatusEnum.REVIEW_FAIL == withdrawDoRequest.getReviewStatus()){//审核不通过,异步通知业务方
		}
		TransactionWithdraw transactionWithdrawDo = transactionWithdrawService.get(withdrawDoDb.getId());
		return transactionWithdrawDo;
	}
	@Override
	protected void referenceData(HttpServletRequest request, Map<String, Object> model) {
		model.put("allReviewStatuss", ReviewStatusEnum.mapping());
		model.put("allNotifyStatuss", NotifyStatusEnum.mapping());
		model.put("allTransStatuss", TransStatusEnum.mapping());
		List<CertTypeEnum> certTypeEnums =  CertTypeEnum.getAllEnum();
		Map<String, String> allRecCertTypes = Maps.newHashMap();
		for(CertTypeEnum certTypeEnum :certTypeEnums){
			allRecCertTypes.put(certTypeEnum.name(),certTypeEnum.getMessage());
		}
		model.put("allRecCertTypes", allRecCertTypes);
		model.put("allCurrencys", CurrencyEnum.mapping());
		model.put("allRecCardTypes", CardTypeEnum.mapping());
		model.put("allRecAccountTypes", PersonalCorporateTypeEnum.mapping());
		model.put("allStatuss", ResultStatus.mapping());
	}

}
